package com.amos.muexamcore.core;

import com.amos.bean.Result;
import com.amos.core.PageDTO;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamcore
 * @ClassName BaseController
 * @Description 通用的分页Controll类
 * @Author Amos
 * @Modifier
 * @Date 2020/3/8 23:06
 * @Version 1.0
 **/
public abstract class BasePageController<T extends PageDTO> {
    /**
     * 获取业务方的PageService
     *
     * @return
     */
    public abstract BasePageService<T> getPageService();

    /**
     * 分页
     *
     * @param dto
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody(required = false) T dto) {
        PageInfo page = getPageService().page(dto);
        return Result.success(page);
    }

    /**
     * 根据id加载
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result load(@PathVariable("id") String id) {
        return Result.success(getPageService().load(id));
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return Result.success(getPageService().logicDeleted(id));
    }

    /**
     * 条件查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/select")
    public Result select(@RequestBody T dto) {
        return Result.success(getPageService().select(dto));
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody T dto) {
        return Result.success(getPageService().save(dto));
    }
}
